package com.net.oil.user;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.base.BaseActivity;
import com.net.oil.user.contract.ChangeLoginPasswordContract;
import com.net.oil.user.presenter.ChangeLoginPasswordPresenter;
import com.net.oil.util.StringUtil;
import com.net.oil.widget.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeLoginPasswordActivity extends BaseActivity<ChangeLoginPasswordPresenter> implements ChangeLoginPasswordContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.confirm)
    Button confirm;
    @BindView(R.id.old_password_et)
    EditText oldPasswordEt;
    @BindView(R.id.news_password_et)
    EditText newsPasswordEt;
    @BindView(R.id.confirm_password_et)
    EditText confirmPasswordEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_login_password);
        ButterKnife.bind(this);
        presenter = new ChangeLoginPasswordPresenter(this);
        initView();
    }

    private void initView() {
        toolbarTitle.setText(R.string.change_login_password);
    }

    @OnClick({R.id.toolbar_go_back, R.id.confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_go_back:
                finish();
                break;
            case R.id.confirm:
                changePassword();
                break;
        }
    }

    private void changePassword() {
        if (!StringUtil.isEmpty(oldPasswordEt.getText().toString())){
            if (!StringUtil.isEmpty(newsPasswordEt.getText().toString())){
                if (!StringUtil.isEmpty(confirmPasswordEt.getText().toString())){
                    if (confirmPasswordEt.getText().toString().equals(newsPasswordEt.getText().toString())){
                        showProgressDialog();
                        presenter.changePassword(oldPasswordEt.getText().toString(), newsPasswordEt.getText().toString());
                    }else {
                        ToastUtil.showErrorToast(this, "密码不一致");
                    }
                }else {
                    ToastUtil.showErrorToast(this, "请确认密码");
                }
            }else {
                ToastUtil.showErrorToast(this, "请输入新密码");
            }
        }else {
            ToastUtil.showErrorToast(this, "请输入旧密码");
        }
    }

    /**
     * 修改密码成功
     */
    @Override
    public void changePasswordSuccess() {
        dismissProgressDialog();
        ToastUtil.showSuccessToast(this, "修改密码成功");
    }

    /**
     * 修改密码失败
     */
    @Override
    public void changePasswordFailed() {
        dismissProgressDialog();
        ToastUtil.showErrorToast(this, "修改密码失败");
    }

}
